package com.qf.service.impl;

import com.qf.dao.EmpDao;
import com.qf.dao.impl.EmpDaoImpl;
import com.qf.entity.Emp;
import com.qf.service.EmpService;
import com.qf.utils.PageUtils;

import java.sql.SQLException;
import java.util.List;

public class EmpServiceImpl implements EmpService {

    private EmpDao empDao=new EmpDaoImpl();


    @Override
    public PageUtils<Emp> selectEmp(String e_name, String e_phone_number, String pageSizeStr, String currentPageNoStr) {
        int pageSize=2;
        if (pageSizeStr==null || "".equals(pageSizeStr)){
            pageSize=2;
        }else {
            pageSize=Integer.parseInt(pageSizeStr);
        }

        int currentPageNo=1;
        if (currentPageNoStr==null || "".equals(currentPageNoStr)){
            currentPageNo=1;
        }else {
            currentPageNo=Integer.parseInt(currentPageNoStr);
        }

        PageUtils<Emp> pageUtils= null;
        try {
            int totalPageCount = empDao.selectEmpCount(e_name, e_phone_number);
            List<Emp> empList = empDao.selectEmp(e_name, e_phone_number, pageSize, currentPageNo);
            int totalPageSize=totalPageCount%pageSize==0? totalPageCount/pageSize:totalPageCount/pageSize+1;


            pageUtils = new PageUtils<>();
            pageUtils.setCurrentPageNo(currentPageNo);
            pageUtils.setList(empList);
            pageUtils.setPageSize(pageSize);
            pageUtils.setTotalPageCount(totalPageCount);
            pageUtils.setTotalPageSize(totalPageSize);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return pageUtils;
    }

    @Override
    public Emp selectEmpById(int e_id) {
        try {
            return empDao.selectEmpById(e_id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int updateEmp(Emp emp) {
        try {
            return empDao.updateEmp(emp);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return -1;
    }

    @Override
    public int addEmp(Emp emp) {
        try {
            return empDao.addEmp(emp);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return -1;
    }

    @Override
    public int deleteEmp(int e_id) {
        try {
            return empDao.deleteEmp(e_id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return -1;
    }
}
